package com.sky.zzh.sleepset;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

import com.next.intf.ITaskListener;
import com.next.net.SHPut;
import com.next.net.SHTask;
import com.sky.widget.SHDialog;
import com.sky.widget.SHDialog.DialogItemClickListener;
import com.sky.widget.material.switchbutton.MaterialSwitchButton;
import com.sky.zzh.R;
import com.sky.zzh.SHApplication;
import com.sky.zzh.util.CommonUtil;
import com.sky.zzh.util.ConfigDefinition;
import com.sky.zzh.util.UserInfoManager;
import com.sky.zzh.view.SimpleTimeDialog;
import com.sky.zzh.view.SimpleTimeDialog.SimpleTimeResultListener;
import com.sky.zzh.view.SlidingMenu;

/**
 * 睡眠设置
 * 
 * @author skypan
 * 
 */
public class SleepSetFragment extends Fragment implements ITaskListener{

	private TextView mTvStart, mTvEnd, mTvSleepNaozhong;
	private MaterialSwitchButton  mSbNaozhong, mSb15, mSb35;
	private String time;
	private SHPut updateTask;
	private TextView mLabelJiushui, mLabelLichuang;
	String[] items_hour = new String[] { "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23" };

	String[] items_hour2 = new String[] { "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11" };

	String[] items_min = new String[] { "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25",
			"26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56",
			"57", "58", "59" };

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.fragment_sleep_set, container, false);
		return view;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onViewCreated(view, savedInstanceState);

		mTvStart = (TextView) view.findViewById(R.id.tv_start);
		mTvEnd = (TextView) view.findViewById(R.id.tv_end);
		mTvSleepNaozhong = (TextView) view.findViewById(R.id.tv_sleep_naozhong);

//		mSbTimeSet = (MaterialSwitchButton) view.findViewById(R.id.sb_time_set);
		mSbNaozhong = (MaterialSwitchButton) view.findViewById(R.id.sb_naozhong);
		mSb15 = (MaterialSwitchButton) view.findViewById(R.id.sb_15);
		mSb35 = (MaterialSwitchButton) view.findViewById(R.id.sb_35);
		mLabelJiushui = (TextView) view.findViewById(R.id.label_15);
		mLabelLichuang = (TextView) view.findViewById(R.id.label_30);

		init();
		setListeners();
	}

	private void init() {
//		if (UserInfoManager.getInstance().isCustomer()) {
//			mSbTimeSet.setChecked(true);
//			mTvStart.setClickable(true);
//			mTvEnd.setClickable(true);
//		} else {
//			mSbTimeSet.setChecked(false);
//			mTvStart.setClickable(false);
//			mTvEnd.setClickable(false);
//		}

//		if (!CommonUtil.isEmpty(UserInfoManager.getInstance().getJiushui())) {
//			mLabelJiushui.setText(UserInfoManager.getInstance().getJiushui());
//		}

//		if (!CommonUtil.isEmpty(UserInfoManager.getInstance().getLichuang())) {
//			mLabelLichuang.setText(UserInfoManager.getInstance().getLichuang());
//		}

		if (!CommonUtil.isEmpty(UserInfoManager.getInstance().getStartTime())) {
			mTvStart.setText(UserInfoManager.getInstance().getStartTime());
		}

		if (!CommonUtil.isEmpty(UserInfoManager.getInstance().getEndTime())) {
			mTvEnd.setText(UserInfoManager.getInstance().getEndTime());
		}

		if (UserInfoManager.getInstance().isNaozhong()) {
			mSbNaozhong.setChecked(true);
		} else {
			mSbNaozhong.setChecked(false);
		}

		if (!CommonUtil.isEmpty(UserInfoManager.getInstance().getNaozhongTime())) {
			mTvSleepNaozhong.setText(UserInfoManager.getInstance().getNaozhongTime());
		}

		if (!CommonUtil.isEmpty(UserInfoManager.getInstance().getJiushui())) {
//			System.out.println("UserInfoManager.getInstance().getJiushui():"+UserInfoManager.getInstance().getJiushui());
			String[] s = UserInfoManager.getInstance().getJiushui().split(":");
			if(s.length == 2){
				mLabelJiushui.setText("超时" + s[0] + "小时" + s[1] + "分钟");
			}else{
				mLabelJiushui.setText("超时" + s[0] + "小时" + "00分钟");
			}
		}

		if (!CommonUtil.isEmpty(UserInfoManager.getInstance().getLichuang())) {
			mLabelLichuang.setText("超时" + UserInfoManager.getInstance().getLichuang());
		}

		if (UserInfoManager.getInstance().isJiushui()) {
			mSb15.setChecked(true);
		} else {
			mSb15.setChecked(false);
		}

		if (UserInfoManager.getInstance().isLichuang()) {
			mSb35.setChecked(true);
		} else {
			mSb35.setChecked(false);
		}

	}

	private void setListeners() {
		mLabelJiushui.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
//System.out.println("ssss:"+UserInfoManager.getInstance().getJiushui());
				if (!SlidingMenu.isOpen) {
					SimpleTimeDialog dia = new SimpleTimeDialog(getActivity(), UserInfoManager.getInstance().getJiushui(), items_hour2, items_min, new SimpleTimeResultListener() {

						@Override
						public void onSimpleResult(Dialog d, String date) {
							// TODO Auto-generated method stub
							String[] s = date.split(":");
							mLabelJiushui.setText("超时" + s[0] + "小时" + s[1] + "分钟");
							float temp = (Integer.valueOf(s[1]))/60+Integer.valueOf(s[0]);
							System.out.println("temp:"+temp);
							UserInfoManager.getInstance().setJiushui(date);
							UserInfoManager.getInstance().sync(getActivity(), true);
							updateTask = new SHPut();
							updateTask.setUrl(ConfigDefinition.URL + "user/ModifyUserInfo");
							updateTask.setListener(SleepSetFragment.this);
							updateTask.getTaskArgs().put("UserID", UserInfoManager.getInstance().getUserId());
							updateTask.getTaskArgs().put("AlarmTimeSleepTooLong", temp+"");
							updateTask.start();
						}
					});
					dia.show();
				}

			}
		});

		mLabelLichuang.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (!SlidingMenu.isOpen) {

//					SimpleTimeDialog dia = new SimpleTimeDialog(getActivity(), UserInfoManager.getInstance().getLichuang(), items_hour2, items_min, new SimpleTimeResultListener() {
//
//						@Override
//						public void onSimpleResult(Dialog d, String date) {
//							// TODO Auto-generated method stub
//							String[] s = date.split(":");
//							mLabelLichuang.setText("超时" + s[0] + "小时" + s[1] + "分钟");
//							UserInfoManager.getInstance().setJiushui(mLabelLichuang.getText().toString());
//							UserInfoManager.getInstance().sync(getActivity(), true);
//						}
//					});
//					dia.show();
					final String[] items = new String[]{"5秒","15秒","30秒","1分钟","5分钟","10分钟","15分钟"};
					SHDialog.showActionSheet(getActivity(), "超时设置", items, new DialogItemClickListener() {
						
						@Override
						public void onSelected(String result) {
							// TODO Auto-generated method stub
							mLabelLichuang.setText("超时" + result);
							String min = "";
							UserInfoManager.getInstance().setLichuang(result);
							if(result.equals(items[0])){
								UserInfoManager.getInstance().setLichuangTime(5);
								min = "0.08";
							}else if(result.equals(items[1])){
								UserInfoManager.getInstance().setLichuangTime(15);
								min = "0.25";
							}else if(result.equals(items[2])){
								UserInfoManager.getInstance().setLichuangTime(30);
								min = "0.5";
							}else if(result.equals(items[3])){
								UserInfoManager.getInstance().setLichuangTime(60);
								min = "1";
							}else if(result.equals(items[4])){
								UserInfoManager.getInstance().setLichuangTime(300);
								min = 5 +"";
							}else if(result.equals(items[5])){
								UserInfoManager.getInstance().setLichuangTime(600);
								min = 10 +"";
							}else if(result.equals(items[6])){
								UserInfoManager.getInstance().setLichuangTime(900);
								min = 15 +"";
							}
							UserInfoManager.getInstance().sync(getActivity(), true);
							updateTask = new SHPut();
							updateTask.setUrl(ConfigDefinition.URL + "user/ModifyUserInfo");
							updateTask.setListener(SleepSetFragment.this);
							updateTask.getTaskArgs().put("UserID", UserInfoManager.getInstance().getUserId());
							updateTask.getTaskArgs().put("AlarmTimeOutOfBed", min);
							updateTask.start();
						}
					});
				}
			}
		});

//		mSbTimeSet.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//
//			@Override
//			public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
//				// TODO Auto-generated method stub
//				if (arg1) {
//					UserInfoManager.getInstance().setCustomer(true);
//					UserInfoManager.getInstance().sync(getActivity(), true);
//					mTvStart.setClickable(true);
//				} else {
//					UserInfoManager.getInstance().setCustomer(false);
//					UserInfoManager.getInstance().sync(getActivity(), true);
//					mTvStart.setClickable(false);
//				}
//			}
//		});

		mTvStart.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (!SlidingMenu.isOpen) {
					SimpleTimeDialog dia = new SimpleTimeDialog(getActivity(), UserInfoManager.getInstance().getStartTime(), items_hour, items_min, new SimpleTimeResultListener() {

						@Override
						public void onSimpleResult(Dialog d, String date) {
							// TODO Auto-generated method stub
							mTvStart.setText(date);
							UserInfoManager.getInstance().setStartTime(date);
							UserInfoManager.getInstance().sync(getActivity(), true);
							updateTask = new SHPut();
							updateTask.setUrl(ConfigDefinition.URL + "user/ModifyUserInfo");
							updateTask.setListener(SleepSetFragment.this);
							updateTask.getTaskArgs().put("UserID", UserInfoManager.getInstance().getUserId());
							updateTask.getTaskArgs().put("SleepStartTime", date);
							updateTask.start();
							Intent intent = new Intent();
							intent.setAction("update_time");
							getActivity().sendBroadcast(intent);
						}
					});
					dia.show();
				}
			}
		});

		mTvEnd.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				SimpleTimeDialog dia = new SimpleTimeDialog(getActivity(), UserInfoManager.getInstance().getEndTime(), items_hour, items_min, new SimpleTimeResultListener() {

					@Override
					public void onSimpleResult(Dialog d, String date) {
						// TODO Auto-generated method stub
						mTvEnd.setText(date);
						UserInfoManager.getInstance().setEndTime(date);
						UserInfoManager.getInstance().sync(getActivity(), true);
						updateTask = new SHPut();
						updateTask.setUrl(ConfigDefinition.URL + "user/ModifyUserInfo");
						updateTask.setListener(SleepSetFragment.this);
						updateTask.getTaskArgs().put("UserID", UserInfoManager.getInstance().getUserId());
						updateTask.getTaskArgs().put("SleepEndTime", date);
						updateTask.start();
						Intent intent = new Intent();
						intent.setAction("update_time");
						getActivity().sendBroadcast(intent);
					}
				});
				dia.show();
			}
		});

		mSbNaozhong.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
				// TODO Auto-generated method stub
				if (arg1) {
					UserInfoManager.getInstance().setNaozhong(true);
					UserInfoManager.getInstance().sync(getActivity(), true);
					SHApplication.getInstance().startNaozhong();
				} else {
					UserInfoManager.getInstance().setNaozhong(false);
					UserInfoManager.getInstance().sync(getActivity(), true);
					SHApplication.getInstance().cancelNaozhong();
				}
			}
		});

		mTvSleepNaozhong.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (!SlidingMenu.isOpen) {
					SimpleTimeDialog dia = new SimpleTimeDialog(getActivity(), UserInfoManager.getInstance().getNaozhongTime(), items_hour, items_min, new SimpleTimeResultListener() {

						@Override
						public void onSimpleResult(Dialog d, String date) {
							// TODO Auto-generated method stub
							mTvSleepNaozhong.setText(date);
							UserInfoManager.getInstance().setNaozhongTime(date);
							UserInfoManager.getInstance().sync(getActivity(), true);
						}
					});
					dia.show();
				}

			}
		});
	}

	@Override
	public void onTaskFinished(SHTask task) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onTaskFailed(SHTask task) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onTaskUpdateProgress(SHTask task, int count, int total) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onTaskTry(SHTask task) {
		// TODO Auto-generated method stub
		
	}
}
